package com.weixin.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.weixin.config.WeiXinProperties;
import com.weixin.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zjd
 * @Title:
 * @Description:
 * @date 2021/4/1417:59
 */
@Api("企业微信客户联系-企业服务人员管理")
@RestController
@RequestMapping("/v1.0/wei-xin/cgi-bin/externalcontact")
public class ExternalContactController {

    @Autowired
    private RestTemplate restTemplate;
    @Resource
    private ObjectMapper objectMapper;

    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation("获取配置了客户联系功能的成员列表")
    @GetMapping("/getFollowUserList")
    public ResponseEntity getFollowUserList() throws IOException {
        StringBuilder urlBuilder=new StringBuilder(WeiXinProperties.getUrl());
        urlBuilder.append("cgi-bin/externalcontact/get_follow_user_list?");
        String access_token = (String) redisUtil.get("access_token_customer");
        urlBuilder.append("access_token="+access_token);
        String result = restTemplate.getForObject(urlBuilder.toString(), String.class);
        Map<String, Object> map = objectMapper.readValue(result, HashMap.class);
        return resultMap(map,1,null);
    }


    @ApiOperation("配置客户联系「联系我」方式")
    @PostMapping("/addContactWay")
    public ResponseEntity addContactWay(@RequestParam("type") String type,
                                        @RequestParam("scene") String scene,
                                        @RequestParam("userList")List<String> user) throws IOException {
        Map  map= new HashMap<String, Object>();
        map.put("type",type);
        map.put("user",user);
        map.put("scene",scene);
        String jsonStr = objectMapper.writeValueAsString(map);
        StringBuilder urlBuilder=new StringBuilder(WeiXinProperties.getUrl());
        urlBuilder.append("cgi-bin/externalcontact/add_contact_way?");
        String access_token = (String) redisUtil.get("access_token_customer");
        urlBuilder.append("access_token="+access_token);
        HttpHeaders httpHeaders = new HttpHeaders();
        // 设置请求类型
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        // 封装参数和头信息
        HttpEntity<String> httpEntity = new HttpEntity<>(jsonStr, httpHeaders);
        String result = restTemplate.postForObject(urlBuilder.toString(),httpEntity, String.class);
        Map<String, Object> readValue = objectMapper.readValue(result, HashMap.class);
        return resultMap(readValue,1,null);
    }


    @ApiOperation("获取配置客户联系「联系我」方式")
    @GetMapping("/getContactWay")
    public ResponseEntity getContactWay(@RequestParam("id") String id) throws IOException {
        Map  map= new HashMap<String, Object>();
        map.put("config_id",id);
        String jsonStr = objectMapper.writeValueAsString(map);
        StringBuilder urlBuilder=new StringBuilder(WeiXinProperties.getUrl());
        urlBuilder.append("cgi-bin/externalcontact/get_contact_way?");
        String access_token = (String) redisUtil.get("access_token_customer");
        urlBuilder.append("access_token="+access_token);
        HttpHeaders httpHeaders = new HttpHeaders();
        // 设置请求类型
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        // 封装参数和头信息
        HttpEntity<String> httpEntity = new HttpEntity<>(jsonStr, httpHeaders);
        String result = restTemplate.postForObject(urlBuilder.toString(),httpEntity, String.class);
        Map<String, Object> readValue = objectMapper.readValue(result, HashMap.class);
        return resultMap(readValue,1,null);
    }

    @ApiOperation("更新配置客户联系「联系我」方式")
    @PutMapping("/updateContactWay")
    public ResponseEntity updateContactWay(@RequestParam("id") String id,
                                           @RequestParam("type") String type,
                                           @RequestParam("scene") String scene,
                                           @RequestParam("userList")List<String> user) throws IOException {
        Map  map= new HashMap<String, Object>();
        map.put("config_id",id);
        map.put("type",type);
        map.put("user",user);
        map.put("scene",scene);
        String jsonStr = objectMapper.writeValueAsString(map);
        StringBuilder urlBuilder=new StringBuilder(WeiXinProperties.getUrl());
        urlBuilder.append("cgi-bin/externalcontact/update_contact_way?");
        String access_token = (String) redisUtil.get("access_token_customer");
        urlBuilder.append("access_token="+access_token);
        HttpHeaders httpHeaders = new HttpHeaders();
        // 设置请求类型
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        // 封装参数和头信息
        HttpEntity<String> httpEntity = new HttpEntity<>(jsonStr, httpHeaders);
        String result = restTemplate.postForObject(urlBuilder.toString(),httpEntity, String.class);
        Map<String, Object> readValue = objectMapper.readValue(result, HashMap.class);
        return resultMap(readValue,1,null);
    }

    @ApiOperation("删除配置客户联系「联系我」方式")
    @DeleteMapping("/delContactWay")
    public ResponseEntity delContactWay(@RequestParam("id") String id) throws IOException {
        Map  map= new HashMap<String, Object>();
        map.put("config_id",id);
        String jsonStr = objectMapper.writeValueAsString(map);
        StringBuilder urlBuilder=new StringBuilder(WeiXinProperties.getUrl());
        urlBuilder.append("cgi-bin/externalcontact/del_contact_way?");
        String access_token = (String) redisUtil.get("access_token_customer");
        urlBuilder.append("access_token="+access_token);
        HttpHeaders httpHeaders = new HttpHeaders();
        // 设置请求类型
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        // 封装参数和头信息
        HttpEntity<String> httpEntity = new HttpEntity<>(jsonStr, httpHeaders);
        String result = restTemplate.postForObject(urlBuilder.toString(),httpEntity, String.class);
        Map<String, Object> readValue = objectMapper.readValue(result, HashMap.class);
        return resultMap(readValue,1,null);
    }


    @ApiOperation("根据用户id获取客户列表")
    @GetMapping("/getList")
    public ResponseEntity getList(@RequestParam("userId") String userId) throws IOException {
        StringBuilder urlBuilder=new StringBuilder(WeiXinProperties.getUrl());
        urlBuilder.append("cgi-bin/externalcontact/list?");
        String access_token = (String) redisUtil.get("access_token_customer");
        urlBuilder.append("access_token="+access_token);
        urlBuilder.append("&userid="+userId);
        String result = restTemplate.getForObject(urlBuilder.toString(), String.class);
        Map<String, Object> map = objectMapper.readValue(result, HashMap.class);
        return resultMap(map,1,null);
    }

    @ApiOperation("获取企业标签库")
    @PostMapping("/getCorpTagList")
    public ResponseEntity getCorpTagList(@RequestParam(value = "tagId",required = false) List<String> tagId,
                                         @RequestParam(value = "groupId",required = false) List<String> groupId) throws IOException {
        Map  map= new HashMap<String, Object>();
        map.put("tag_id",tagId);
        map.put("group_id",groupId);
        String jsonStr = objectMapper.writeValueAsString(map);
        StringBuilder urlBuilder=new StringBuilder(WeiXinProperties.getUrl());
        urlBuilder.append("cgi-bin/externalcontact/get_corp_tag_list?");
        String access_token = (String) redisUtil.get("access_token_customer");
        urlBuilder.append("access_token="+access_token);
        HttpHeaders httpHeaders = new HttpHeaders();
        // 设置请求类型
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        // 封装参数和头信息
        HttpEntity<String> httpEntity = new HttpEntity<>(jsonStr, httpHeaders);
        String result = restTemplate.postForObject(urlBuilder.toString(),httpEntity, String.class);
        Map<String, Object> readValue = objectMapper.readValue(result, HashMap.class);
        return resultMap(readValue,1,null);
    }

    private ResponseEntity resultMap(Map<String, Object> resultMap, Integer type,String key){
        for (Map.Entry<String, Object> entry : resultMap.entrySet()) {
            if (entry.getKey().equals("errcode")){
                Integer integer = (Integer) resultMap.get("errcode");
                if (integer==0){
                    ResponseEntity responseEntity = type==0 ? new ResponseEntity(resultMap.get(key), HttpStatus.OK): new ResponseEntity(resultMap, HttpStatus.OK);
                    return responseEntity;
                }
            }
        }
        return new ResponseEntity(resultMap,HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
